package com.juzipi.demo.config;


import com.juzipi.demo.pojo.Users;
import com.juzipi.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 云E办
 */


@Configuration
//@EnableWebSecurity// 开启WebSecurity模式
public class TestSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private AdminService adminService;

    @Override
    public UserDetailsService userDetailsServiceBean() throws Exception {
        return username->{

            Users users = adminService.getAdminByUserName(username);
            if (users!=null){
                return users;
            }
            return null;
        };
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        使用jwt不需要csrf
        http.csrf()
                .disable()
//                基于token不需要Session
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
//                允许登录访问
                .antMatchers("/login","logout")
                //除了登录和退出所有请求都要认证
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .headers()
                .cacheControl();
        //添加jwt,登录授权过滤器
        http.addFilterBefore(jwtAuthencationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
        //添加自定义未授权和未登录返回结果
        http.exceptionHandling()
                .accessDeniedHandler()
                .authenticationEntryPoint();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }


    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    @Bean
    public JwtAuthencationTokenFilter jwtAuthencationTokenFilter(){
        return new JwtAuthencationTokenFilter();
    }







    /*
    FilterSecurityInterceptor: 是一个方法级的权限过滤器，基本位于过滤器最底部
    ExceptionTranslationFilter: 是一个异常过滤器，用来处理在认证授权过程中抛出的异常
    UsernamePasswordAuthenticationFilter: 对/login的POST请求做拦截，校验表单中的用户名密码



     */
}
